package com.boot.system.controller;

import com.boot.framework.aop.Log;
import com.boot.framework.common.R;
import com.boot.system.dto.SysMenuDTO;
import com.boot.system.wrapper.SysMenuWrapper;
import com.boot.system.service.ISysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author yangfan
 * @date 2023/12/22
 * @Description
 */

@RestController
@AllArgsConstructor
@RequestMapping("/system/menu")
@Tag(name = "文件管理")
public class SysMenuController {
    private ISysMenuService sysMenuService;

    @GetMapping("list")
    @Operation(summary = "集合")
    public R list(@RequestParam Map<String, Object> params) {
        return R.ok(sysMenuService.selectList(params));
    }

    /**
     * 新增菜单
     */
    @PostMapping
    @Log(title = "菜单管理", content = "新增")
    @Operation(summary = "新增")
    public R insert(@RequestBody SysMenuDTO sysMenuDTO) {
        return R.ok(sysMenuService.save(SysMenuWrapper.build().dtoEntity(sysMenuDTO)));
    }

    /**
     * 修改菜单
     *
     * @param sysMenuDTO
     * @return
     */
    @PutMapping
    @Log(title = "菜单管理", content = "修改")
    @Operation(summary = "修改")
    public R update(@RequestBody SysMenuDTO sysMenuDTO) {
        return R.ok(sysMenuService.updateById(SysMenuWrapper.build().dtoEntity(sysMenuDTO)));
    }

    /**
     * 查询菜单
     */
    @GetMapping
    @Operation(summary = "查看")
    public R view(@RequestParam("id") String id) {
        return R.ok(SysMenuWrapper.build().entityDTO(sysMenuService.getById(id)));
    }

    /**
     * 删除菜单
     */
    @DeleteMapping
    @Log(title = "菜单管理", content = "删除")
    @Operation(summary = "删除")
    public R delete(String ids) {
        return R.ok(sysMenuService.removeBatchByIds(Arrays.asList(StringUtils.split(ids, ","))));
    }

    @PostMapping("/list/roleIds")
    @Operation(summary = "集合")
    R<List<SysMenuDTO>> selectListByRoleIds(@RequestBody SysMenuDTO sysMenuDTO, @RequestBody List<String> roleIds) {
        return R.ok(sysMenuService.selectListByRoleIds(sysMenuDTO, roleIds));
    }
}
